package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.FriendDto;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.user.TouristsUserDto;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/28 20:17
 */
public class ModifyRemarkView extends IView {

    private ModifyRemarkCallback mCallback;

    public ModifyRemarkView(ModifyRemarkCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doModifyRemark(String remark) {
        int action;
        String data;
        if (UserPreferences.isRegistered()) {
            action = EventConstants.Action.PERSONAL_NICKNAME_REGISTERED;
            RegisteredUserDto registeredUser = new RegisteredUserDto();
            registeredUser.setUserId(UserPreferences.getUserId());
            registeredUser.setNickname(remark);
            data = registeredUser.toJson();
        } else {
            action = EventConstants.Action.PERSONAL_NICKNAME_TOURISTS;
            TouristsUserDto touristsUser = new TouristsUserDto();
            touristsUser.setTemporaryId(UserPreferences.getUserId());
            touristsUser.setNickname(remark);
            data = touristsUser.toJson();
        }
        doSendRequest(action, data);
    }

    public void doModifyFriendAlias(String friendUserId, String friendUserType, String friendAlias) {
        FriendDto friendDto = new FriendDto(UserPreferences.getUserType());
        friendDto.setFriendUserId(friendUserId);
        friendDto.setUserType(friendUserType);
        friendDto.setFriendAlias(friendAlias);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_ALIAS_REGISTERED : EventConstants.Action.FRIEND_ALIAS_TOURISTS;
        doSendRequest(action, friendDto.toJson());
    }

    public void doModifyGroupName(String groupId, String newGroupName) {
        GroupDto groupDto = new GroupDto(UserPreferences.getUserType());
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(newGroupName);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_NAME_REGISTERED : EventConstants.Action.GROUP_NAME_TOURISTS;
        doSendRequest(action, groupDto.toJson());
    }

    public void doModifyGroupMemberAlias(String groupId, String memberId, String groupMemberId, String groupMemberAlias) {
        GroupMemberDto groupMemberDto = new GroupMemberDto(UserPreferences.getUserType());
        groupMemberDto.setGroupMemberId(memberId);
        GroupDto groupDto = new GroupDto(groupMemberDto.getCurrentUserType());
        groupDto.setGroupId(groupId);
        groupMemberDto.setGroupMemberUserId(groupMemberId);
        groupMemberDto.setGroup(groupDto);
        groupMemberDto.setGroupMemberAlias(groupMemberAlias);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_MEMBER_ALIAS_REGISTERED : EventConstants.Action.GROUP_MEMBER_ALIAS_TOURISTS;
        doSendRequest(action, groupMemberDto.toJson());
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        Logger.print("ModifyRemarkView", event.toString());
        if (EventConstants.Action.PERSONAL_NICKNAME_TOURISTS == event.action()) {
            UserDto user = null;
            if (checkIsSuccess(event.code(), event.data())) {
                TouristsUserDto touristsUser = Dto.fromJson(event.data(), TouristsUserDto.class);
                if (null != touristsUser) user = new UserDto(touristsUser);
            }
            if (null != mCallback)
                mCallback.onModifyRemark(event.code(), event.msg(), user);
        } else if (EventConstants.Action.PERSONAL_NICKNAME_REGISTERED == event.action()) {
            UserDto user = null;
            if (checkIsSuccess(event.code(), event.data())) {
                RegisteredUserDto registeredUser = Dto.fromJson(event.data(), RegisteredUserDto.class);
                if (null != registeredUser) user = new UserDto(registeredUser);
            }
            if (null != mCallback)
                mCallback.onModifyRemark(event.code(), event.msg(), user);
        } else if (EventConstants.Action.FRIEND_ALIAS_TOURISTS == event.action() ||
                EventConstants.Action.FRIEND_ALIAS_REGISTERED == event.action()) {
            FriendDto friend = null;
            if (checkIsSuccess(event.code(), event.data()))
                friend = Dto.fromJson(event.data(), FriendDto.class);
            if (null != mCallback)
                mCallback.onModifyFriendAlias(event.code(), event.msg(), friend);
        } else if (EventConstants.Action.GROUP_NAME_TOURISTS == event.action() ||
                EventConstants.Action.GROUP_NAME_REGISTERED == event.action()) {
            GroupDto groupDto = null;
            if (checkIsSuccess(event.code(), event.data()))
                groupDto = Dto.fromJson(event.data(), GroupDto.class);
            if (null != mCallback)
                mCallback.onModifyGroupName(event.code(), event.msg(), groupDto);
        } else if (EventConstants.Action.GROUP_MEMBER_ALIAS_TOURISTS == event.action() ||
                EventConstants.Action.GROUP_MEMBER_ALIAS_REGISTERED == event.action()) {
            GroupMemberDto groupMemberDto = null;
            if (checkIsSuccess(event.code(), event.data()))
                groupMemberDto = Dto.fromJson(event.data(), GroupMemberDto.class);
            if (null != mCallback)
                mCallback.onModifyGroupMemberAlias(event.code(), event.msg(), groupMemberDto);
        }
    }

    public interface ModifyRemarkCallback {

        // 修改个人昵称
        void onModifyRemark(int code, String msg, UserDto data);

        // 修改好友备注
        void onModifyFriendAlias(int code, String msg, FriendDto data);

        // 修改群名称
        void onModifyGroupName(int code, String msg, GroupDto data);

        // 修改群成员备注
        void onModifyGroupMemberAlias(int code, String msg, GroupMemberDto data);
    }
}
